package com.mypaper.list;

import com.mypaper.elem.DataElement;

public class StackClass {
	private int maxStackSize;
	private int stackTop;
	private DataElement[] list;
	
	public StackClass(int maxStackSize){
		this.maxStackSize=maxStackSize;
		this.stackTop=0;
		this.list=new DataElement[maxStackSize];
		for(int i=0; i<stackTop; i++){
			this.list[i]=null;
		}
	}
	
	public void initalize(int maxStackSize){
		if(maxStackSize<=0){
			System.out.println("The size must be positive.");
		}else{
			this.maxStackSize=maxStackSize;
		}
	}
	
	public boolean isEmpty(){
		return (stackTop==0);
	}
	
	public boolean isFullStack(){
		return (stackTop==maxStackSize);
	}
	
	public void push(DataElement newItem){
		if(isFullStack())
			System.out.println("The stack is full.");
		else {
			list[stackTop]=newItem.getCopy();
			stackTop++;
		}
	}
	
	public DataElement top(){
		if(isEmpty()){
			System.out.println("The stack is empty.");
			return null;
		}else {
			DataElement temp=list[stackTop-1].getCopy();
			return temp;
		}
	}
	
	public void pop(){
		if(isEmpty()){
			System.out.println("The stack is empty.");
		}else {
			stackTop--;
			list[stackTop]=null;
		}
	}	
	
	public void print(){
		while(stackTop>0){
			DataElement Top=top();
			System.out.println(Top);
			pop();
		}
	}
}
